
var displayStart = 1;
var endPage = null;
var layer = layui.layer;
var form = null;
var list = null;
var table=layui.table;
var isSelect=0;//选择标识符

layui.use('upload', function(){
    var upload = layui.upload;

    //执行实例
    var uploadInst = upload.render({
        elem: '#test1' //绑定元素
        ,url: '/upload/' //上传接口
        ,done: function(res){
            //上传完毕回调
        }
        ,error: function(){
            //请求异常回调
        }
    });
});

function changePages(pageNum){
    $('#displayLength').val(pageNum)
    search(1);
}

//筛选我的待办方法
function screenOrder() {
    //未选中状态
    if(isSelect===0){
        //筛选数据
        $.ajax({
            url: url + '/work-order/myOrderTips',
            method: 'POST',
            async:false,
            contentType: 'application/json',
            data: JSON.stringify({
                userId:sessionStorage.getItem('userId'),
                deptId:""
            }),
            success: function (res) {
                if(res.code===400){
                    layer.msg("..暂无任务分配到您..")
                    //列表筛选
                    $('#orderNum').val("");
                    $('#productType').val("");
                    $('#createBy').val("");
                    $('#workshop').val("");
                    $('#deptName').val("");
                    $('#status').val("0");
                    $('#workerNo').val(sessionStorage.getItem('loginName'));
                    search(1);
                } else{
                    if(isSelect!==0){//筛选在选中状态下
                        //样式复原
                        $('#screenIcon').html("&#xe63f;");
                        $('#screenFont').css("color","#000000");
                        //查询按钮复原
                        $('#searchBtn').attr('disabled',false);
                        $('#searchBtn').css("cursor","pointer");
                        isSelect=0;//恢复标识符
                    }
                    //开始筛选
                    $('#orderNum').val("");
                    $('#productType').val("");
                    $('#createBy').val("");
                    $('#workshop').val("");
                    $('#deptName').val("");
                    $('#status').val("0");//进行中
                    $('#workerNo').val(sessionStorage.getItem('loginName'));//属于我的待办
                    search(1);
                    layer.msg("为你筛选出"+res.data+"条待办工单,请您及时完成!")
                }
            }
        })
        //样式改变
        $('#screenIcon').html("&#xe643;");
        $('#screenFont').css("color","#01AAED");
        //查询按钮禁用
        $('#searchBtn').attr('disabled',true);
        $('#searchBtn').css("cursor","no-drop");
        isSelect=1;//标识符选中状态
    } else{
        //样式改变
        $('#screenIcon').html("&#xe63f;");
        $('#screenFont').css("color","#000000");
        //查询按钮复原
        $('#searchBtn').attr('disabled',false);
        $('#searchBtn').css("cursor","pointer");
        isSelect=0;//标识符归0
        //取消筛选，恢复初始
        $('#orderNum').val("");
        $('#productType').val("");
        $('#createBy').val("");
        $('#workshop').val("");
        $('#deptName').val("");
        $('#status').val("");
        $('#workerNo').val("");
        search(1);
    }
}

//刷新数据
function reloadThis() {
    isSelect=1;
    //执行筛选,模拟刷新
    screenOrder();
    var index = layer.load(2, {
        shade: [0.5,'#fff'] //0.1透明度的白色背景
    });
    setTimeout(function() {
        layer.close(index)
        layer.msg("刷新成功")
    }, 800);
}


function importFile() {
    layer.msg("..暂未开放..")
}

//排序
function sortLotNo(date) {
    var index = layer.load(2, {
        shade: [0.5,'#fff'] //0.1透明度的白色背景
    });
    setTimeout(function() {
        layer.close(index)
    }, 300);
    search(1,date)

}
function sortTime(date) {
    var index = layer.load(2, {
        shade: [0.5,'#fff'] //0.1透明度的白色背景
    });
    setTimeout(function() {
        layer.close(index)
    }, 300);
    search(1,date)
}

//审核
function checkOrder(index) {
    var orderNum;
    //判断
    $.ajax({
        url: url + '/work-order/isOverOfReport/'+index,
        method: 'GET',
        contentType: 'application/json',
        success: function (res) {
            if (res.code == 400) {//不符合审核条件-----进行中OR已完成
                layer.msg(res.msg);
            }else{//符合审核条件-----待审核
                $.ajax({
                    url: url + '/report-order/getReportById/'+index,
                    method: 'GET',
                    contentType: 'application/json',
                    success: function (res) {
                        if(res.code==400){
                            layer.msg(res.msg);
                            return;
                        }
                        orderNum=res.data[0].orderNum;
                        $("#checkBox").empty();
                        var trs='';
                        var amountPlan=0;
                        var amount=0;
                        var badTotalAmount=0;
                        var missingAmount=0;
                        //获取数量
                        $.ajax({
                            url: url + '/report-order/listNum/'+orderNum.replace("#","%23"),
                            contentType: 'application/json',
                            async: false,
                            success: function(res) {
                                amountPlan=res.data.amountPlan;
                                amount=res.data.amount;
                                badTotalAmount=res.data.badTotalAmount;
                                missingAmount=amountPlan-amount-badTotalAmount;
                            }
                        })
                        trs+='<div style="margin: 5px" align="center"><h3><b> 最终数据总计</b> </h3></div>'
                        trs+='<div align="center"><table border="2" width="510px" height="80px">' +
                            '    <tr>' +
                            '        <td><b>计划数量</b></td>' +
                            '        <td><b>结算数量(含余数)</b></td>' +
                            '        <td><b>★ 总不良品数</b></td>' +
                            '        <td><b style="color:#ff1818;">★ 总缺数</b></td>' +
                            '    </tr>' +
                            '    <tr>' +
                            '        <td><b style="color: #1890ff">'+amountPlan+'</b></td>' +
                            '        <td><b style="color: #2b8d08">'+amount+'</b></td>' +
                            '        <td><b style="color: #ff6918">'+badTotalAmount+'</b></td>' +
                            '        <td><b style="color: #ff1818">'+missingAmount+'</b></td>' +
                            '    </tr>' +
                            '</table></div>';
                        for (var i = 0; i < res.data.length; i++) {
                            trs += '<div class="page-content layui-collapse">';
                            trs += '<div class="supplier_style layui-colla-item">';
                            trs += '<div class="info_Content" style="font-size: 15px">';
                            trs +='<h2 class="layui-colla-title"><b style="color: #f5222d"> '+res.data[i].reportProcess+'  </b><b>上报记录</b></h2>';
                            trs +='<div class="layui-colla-content layui-show">';
                            trs += '<ul class="Content_list clearfix">';
                            // trs += '<label class="label_name" style="line-height: 2"><b style="color: #f5222d">——上报记录'+j+':             </b></label>';
                            trs += '<li style="line-height:2.5"><label class="label_name"><b>●  上报工单的工单号:             </b></label><span style="color: #1890ff">'+res.data[0].orderNum+'</span></li>';
                            trs += '<li style="line-height:2.5"><label class="label_name"><b>●  上报人名字:                  </b></label><span style="color: #1890ff">'+res.data[i].reporter+'</span></li>';
                            trs += '<li style="line-height:2.5"><label class="label_name"><b>●  上报的工序:                  </b></label><span style="color: #1890ff">'+res.data[i].reportProcess+'</span></li>';
                            trs += '<li style="line-height:2.5"><label class="label_name"><b>●  上报的良品数量(含余数录入):             </b></label><span style="color: #1890ff">'+res.data[i].reportAmount+'</span></li>';
                            trs += '<li style="line-height:2.5"><label class="label_name"><b>●  上报的不良品数量:                      </b></label><span style="color: #1890ff">'+res.data[i].badAmount+'</span></li>';
                            trs += '<li style="line-height:2.5"><label class="label_name"><b>●  作业机台:                      </b></label><span style="color: #1890ff">'+res.data[i].machineMessage+'</span></li>';
                            trs += '<li style="line-height:2.5"><label class="label_name"><b>●  该工序执行人:                 </b></label><span style="color: #1890ff">'+res.data[i].processWorker+'</span></li>';
                            trs += '<li style="line-height:2.5"><label class="label_name"><b>●  上报时间:                    </b></label><span style="color: #1890ff">'+res.data[i].createTime+'</span></li>';
                            trs += '</ul>';
                            trs += '</div>';
                            trs += '</div>';
                            trs += '</div>';
                            trs += '</div>';
                        }
                        trs+='<div align="center" style="margin: 10px">' +
                            '<button id="checkPass" type="submit" class="layui-btn layui-btn-normal" lay-submit="" lay-filter="checkPass"><b>审核通过</b></button>' +
                            '<button class="layui-btn layui-btn-danger" onclick="layer.close(layer.index)"><b>关闭</b></button>' +
                            '</div>';
                        $("#checkBox").append(trs);
                        layer.open({
                            type: 1,
                            title: '审核工单',
                            maxmin: true,
                            shadeClose: true, //点击遮罩关闭层
                            area : ['600px' , '700px'],
                            content: $('#checkBox'),
                            end:function () {
                                $('#checkBox').css('display','none')
                            }
                        });
                        form.on('submit(checkPass)', function() {
                            $.ajax({
                                url: url + '/check-info/checkPass',
                                method: 'POST',
                                contentType: 'application/json',
                                async: false,
                                data: JSON.stringify({
                                    orderNum:orderNum,
                                    loginName:sessionStorage.getItem('loginName'),
                                    passStatus:'1'
                                }),
                                success: function(res) {
                                    console.log(res);
                                    if (res.code == 200) {
                                        parent.layer.alert('审核完毕', {
                                            end: function(){
                                                location.reload();
                                            }
                                        });
                                    }
                                }
                            })
                            return false;
                        })
                    }
                })
            }
        }
    })
}



layui.use(['form', 'element'], function() {
    form = layui.form;
    $.ajax({
        url: url + '/sys-dept/listDept',
        method: 'POST',
        async : false,
        contentType: 'application/json',
        success: function(res) {
            //显示前先判断是否为管理员
            $.ajax({
                url: '/sys-user/getAdmin/'+sessionStorage.getItem('userId'),
                method: 'GET',
                async : false,
                contentType: 'application/json',
                success: function (res) {
                    if (res.code===200){
                        isAd=1;
                    }
                    else if (res.code===400){
                        isAd=0;
                        thisDept=res.data.dept;
                    }
                }
            })
            //按照是否为管理员显示下拉框
            if (res.code == 200) {
                for (var i = 0; i < res.data.length; i++) {
                    if(isAd===1){
                        $("#deptName").append("<option value='" + res.data[i].id + "'>" + res.data[i].dept +"</option>");
                    }else if (isAd===0&&res.data[i].dept===thisDept){
                        $("#deptName").append("<option selected='selected' value='" + res.data[i].id + "'>" + res.data[i].dept +"</option>").attr('disabled','disabled');
                    }
                }
                form.render('select');
            }
        }
    })
    //品名下拉框
    $.ajax({
        url: url + '/pin-library/listPinName',
        method: 'POST',
        async : false,
        contentType: 'application/json',
        data: JSON.stringify({
            userId:"",
            deptId: $('#deptName').val()
        }),
        success: function(res) {
            if (res.code == 200) {
                for (var i = 0; i < res.data.length; i++) {
                    $("#pinName").append("<option value='" + res.data[i].id + "'>" + res.data[i].pinName +"</option>");
                }
            }
        }
    })
    search(1);
    //下拉框二级联动---点击部门下拉框
    form.on('select(orderDept)', function(data) {
        $("#pinName").empty()
        //品名下拉框
        $.ajax({
            url: url + '/pin-library/listPinName',
            method: 'POST',
            async : false,
            contentType: 'application/json',
            data: JSON.stringify({
                userId:"",
                deptId: $('#deptName').val()
            }),
            success: function(res) {
                $("#pinName").append("<option value=''>请选择品名....</option>");
                if (res.code == 200) {
                    for (var i = 0; i < res.data.length; i++) {
                        $("#pinName").append("<option value='" + res.data[i].id + "'>" + res.data[i].pinName +"</option>");
                    }
                }
            }
        })
        form.render('select');
    });
    form.on('submit(searchBtn)', function() {
        search(1);
        return false;
    });
    $('.fristPage').click(function() {
        search(1);
        return false;
    });
    $('.lastPage').click(function() {
        search((displayStart > 1 ? displayStart - 1 : 1));
        return false;
    });
    $('.nextPage').click(function() {
        search((displayStart < endPage ? displayStart + 1 : endPage));
        return false;
    });
    $('.endPage').click(function() {
        search(endPage);
        return false;
    })

})

function selectAll() {
    $("input[name=downSelect]:not(:disabled)").each(function(){
        this.checked = $("#select")[0].checked
    });
    // $('input[type=checkbox]').each(function() {
    //     var disabled= $("#select")[0].attr("disabled");//获取当前对象的disabled属性
    //     if(disabled==false){ //判断如果是非disabled则进行处理
    //         this.checked = $("#select")[0].checked
    //     }
    // })
}
function selectAllOfCheck() {
    $("input[name=checkSelect]:not(:disabled)").each(function(){
        this.checked = $("#checkSelect")[0].checked
    });
    // $('input[type=checkbox]').each(function() {
    //     var disabled= $("#select")[0].attr("disabled");//获取当前对象的disabled属性
    //     if(disabled==false){ //判断如果是非disabled则进行处理
    //         this.checked = $("#select")[0].checked
    //     }
    // })
}

function downloadFile(index) {
    var list = [];
    $("#downChoose").empty();
    if(!index) {$('input[name=downSelect]').each(function() {
        if(this.checked && this.alt) list.push(this.alt);
    })} else list.push(index);
    if(list.length==0){
        layer.msg("请选择至少一张工单导出！")
    }else {
        //以下方法是一张a4张列印两张时
        // $.ajax({
        //     url: url + '/excel/handleData/' + list,
        //     method: 'GET',
        //     contentType: 'application/json',
        //     async: false,
        //     success: function (res) {
        //         if (res.code===200){
        //             list=res.data;
        //         }
        //     }
        // })
        console.log(list)
        let confirm1=layer.confirm('点击确定开始批量导出！</br>' +
            '★仅支持<b style="color: #F5222D">列票一次</b>！请注意：</br>' +
            '①：请设置浏览器<b style="color: #F5222D">允许弹窗</b>！！！</br>' +
            '②：请设置浏览器<b style="color: #F5222D">允许下载多项</b>！！！', {
            btn: ['确认','取消']
        },function () {
            for(var i=0;i<list.length;i++){
                //window.open(url+'/excel/exportTemplateData/'+list[i]);
                layer.open({
                    type: 2,
                    title: '下载提示框',
                    shadeClose: true,
                    shade: false,
                    maxmin: true, //开启最大化最小化按钮
                    area: ['300px', '300px'],
                    content: url+'/excel/exportTemplateData/'+list[i]
                });
                layer.close(confirm1);
            }
            layer.alert('正在导出..请选择允许下载多项！！！导出后在下载记录里可以找到!', {
                icon: 1,
                skin: 'layer-ext-demo', //见：扩展说明
                end: function () {
                    //关闭弹出
                    layer.closeAll('iframe');
                    search(1)
                }
            })
        },function (){
            layer.close(confirm1);
        })
    }
}



function batchCheckInIt(index) {
    var list = [];
    if(!index) {$('input[name=checkSelect]').each(function() {
        if(this.checked && this.alt) list.push(this.alt);
    })} else list.push(index);
    if(list.length==0){
        layer.msg("请选择至少一张工单检录！")
    }else {
        let confirm1=layer.confirm('确定执行批量检录?', {
            btn: ['确认','取消']
        },function () {
            $.ajax({
                url: url + '/work-order/batchCheckIn/' + list,
                method: 'GET',
                contentType: 'application/json',
                async: false,
                success: function (res) {
                    if(res.code==200){
                        parent.layer.alert(res.msg, {
                            icon: 1,
                            skin: 'layer-ext-demo', //见：扩展说明
                            end: function(){
                                layer.closeAll();
                                $('#checkSelect').removeAttr("checked")
                                search(1);
                            }
                        })
                    }else {
                        parent.layer.alert(res.msg, {
                            icon: 2,
                            skin: 'layer-ext-demo' ,//见：扩展说明
                            end: function(){
                                layer.closeAll();
                                $('#checkSelect').removeAttr("checked")
                                search(1 );
                            }
                        })
                    }
                }
            })
            layer.close(confirm1);
        },function (){
            layer.close(confirm1);
        })
    }
}

function edit(index) {
    var nowWorker=[];
    var orderDept;
    //先判断权限
    $.ajax({
        url: '/sys-user/getAdminLevel/'+sessionStorage.getItem('userId'),
        method: 'GET',
        contentType: 'application/json',
        success: function (res) {
            if (res.code===200){
                //再判断是否完工
                $.ajax({
                    url: url + '/work-order/isOver/' + index,
                    method: 'GET',
                    contentType: 'application/json',
                    async: false,
                    success: function (res) {
                        if (res.code===200){
                            var i = 0;
                            for(; i<list.length; i++){
                                if(list[i].id == index) break;
                            }
                            layer.open({
                                type: 1,
                                skin: 'layui-layer-rim',
                                area: ['400px', '450px'],
                                title: '工单编辑',
                                content: `
            <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
                <ul class="layui-tab-title">
                  <li class="layui-this">工序分解或转交</li>
                  <li>基础信息变更</li>
                </ul>
                <div class="layui-tab-content">
                
                <div class="layui-tab-item  layui-show">
                <form class="layui-form" action="" lay-filter="redirect">
                
                <label class="layui-form-label">目标工序：</label>
                <div class="layui-input-inline " style="width: 15em"> 
                    <select name="goalProcess" id="goalProcess"  lay-verify="required" lay-filter="goalProcess" lay-search="">
                    </select>
                </div>
                <div style=" margin: 15px"></div>
                <label class="layui-form-label">转交或分解：</label>
                <div class="layui-input-inline " style="width: 15em">
                <div id="nowWorker" lay-filter="nowWorker"></div>
                </div>
                
                <div align="center" style="margin:30px 0">
                <button id="redirect" type="submit" class="layui-btn" lay-submit="" lay-filter="redirectBtn" >提交更改</button>
                 <button class="layui-btn layui-btn-danger" onclick="layer.close(layer.index)">取消编辑</button></div>
                 
                </form>
                </div>
                
                <div class="layui-tab-item">
                <form class="layui-form" action="" lay-filter="change">
                <div style=" margin: 15px"></div>

                <input id="orderId" type="text" name="id" style="display: none;">

                <label class="layui-form-label">产品类型：</label>
                <div class="layui-input-inline " style="width: 15em"  >
                <input id="productType" type="text" name="productType" placeholder="输入产品类型" lay-verify="required" class="layui-input" style="width: 18em !important;">
                </div>

                <div style=" margin: 15px"></div>
                <label class="layui-form-label">下达车间：</label>
                <div class="layui-input-inline " style="width: 15em">
                <input id="workshop" type="text" name="workshop" placeholder="输入下达车间" lay-verify="required" class="layui-input" style="width: 18em !important;"></div>
              
                <div style=" margin: 15px"></div>
                <label class="layui-form-label">计划数量：</label>
                <div class="layui-input-inline " style="width: 15em">
                <input id="amountUnit" type="text" onkeyup="value=value.replace(/[^\\d]/g,'')" name="amountUnit" placeholder="输入数量" lay-verify="required" class="layui-input" style="margin-bottom: 10px;width: 15.5em !important;"></div>

                <div align="center" style="margin:30px 0">
                 <button id="submit" type="submit" class="layui-btn" lay-submit="" lay-filter="changeBtn" >点击修改</button>
                 <button class="layui-btn layui-btn-danger" onclick="layer.close(layer.index)">取消编辑</button></div>
                </form>
                
                </div>
                </div>
            </div>      
            
          `,

             success: function(){

                 $.ajax({
                     url: url + '/work-order/putProcess/'+index,
                     contentType: 'application/json',
                     async: false,
                     success: function(res) {
                         for (var i = 0; i < res.data.length; i++) {
                             $("#goalProcess").append("<option value='" + res.data[i].processId + "'>" + res.data[i].processName +
                                 "</option>");
                         }
                         form.render('select');
                     }
                 })

                 $.ajax({
                     url: url + '/work-order/getOrderById/'+index,
                     contentType: 'application/json',
                     async: false,
                     success: function(res) {
                         //得到用户
                         $('#orderId').val(res.data.id);
                         form.val("change",{
                             "productType":res.data.productType,
                             "workshop":res.data.workshop,
                             "amountUnit":res.data.amountPlan,
                         })
                         //得到原先人员
                         nowWorker=res.data.workerNo.split(",")
                         orderDept=res.data.orderDept
                         form.render();
                     }
                 })


                 //默认的指派用户下拉框
                 var userOption = {
                     el: '#nowWorker',
                     name: 'nowWorker',//表单的name属性
                     filterable: true,
                     id: 'reNowWorker',
                     initValue: [ nowWorker[0],nowWorker[1],nowWorker[2],nowWorker[3],nowWorker[4],nowWorker[5],nowWorker[6],nowWorker[7],nowWorker[8],
                         nowWorker[9],nowWorker[10],nowWorker[11],nowWorker[12],nowWorker[13],nowWorker[14],nowWorker[15],nowWorker[16],nowWorker[17],nowWorker[18],
                         nowWorker[19],nowWorker[20],nowWorker[21],nowWorker[22],nowWorker[23],nowWorker[24],nowWorker[25],nowWorker[26],nowWorker[27],nowWorker[28]
                     ],//默认项
                     layVerify: 'required',//必填项
                     //layVerType: 'tips',//提示类型 同layui
                     tips: '请选择工序...',
                     data: [],
                 };

                 var user = xmSelect.render(userOption)

                 setTimeout(function(){
                     $.ajax({
                         url: url + '/sys-user/userList',
                         method: 'POST',
                         async : false,
                         contentType: 'application/json',
                         data: JSON.stringify({
                             userId:"",
                             deptId:orderDept
                         }),
                         success: function(res) {
                             //data是ajax 异步获取的
                             var data =[];
                             for (var i = 0; i < res.data.length; i++) {
                                 data.push({
                                     name : res.data[i].userName,
                                     value :res.data[i].loginName
                                 });
                             }
                             //模拟通过ajax 获取json数据,异步更新多选下拉框的值
                             user.update({data: data})
                         }
                     })
                 },100);

                 form=layui.form;
                 form.render('select');
             }
             });
           }else if (res.code===400){
                layer.msg("行为禁止！已完成或待审核的工单不能修改")
              }
            }
           })
        } else if (res.code===400){
           layer.msg("权限不足!");
         }
      }
    })

    //指派用户下拉框二级联动
    form.on('select(goalProcess)', function(data) {
        //联动数据获取
        $.ajax({
            url: url + '/work-order/listWorkerOfProcess',
            contentType: 'application/json',
            method: 'POST',
            async: false,
            data: JSON.stringify({
                id: index,
                processId: $('#goalProcess').val()
            }),
            success: function(res) {
                if(res.code==200){
                    //得到原先人员
                    nowWorker=res.data.split(",")
                }
                form.render();
            }
        })
        //用户下拉框
        var userOption = {
            el: '#nowWorker',
            name: 'nowWorker',//表单的name属性
            filterable: true,
            id: 'reNowWorker',
            initValue: [ nowWorker[0],nowWorker[1],nowWorker[2],nowWorker[3],nowWorker[4],nowWorker[5],nowWorker[6],nowWorker[7],nowWorker[8],
                nowWorker[9],nowWorker[10],nowWorker[11],nowWorker[12],nowWorker[13],nowWorker[14],nowWorker[15],nowWorker[16],nowWorker[17],nowWorker[18],
                nowWorker[19],nowWorker[20],nowWorker[21],nowWorker[22],nowWorker[23],nowWorker[24],nowWorker[25],nowWorker[26],nowWorker[27],nowWorker[28]
            ],//默认项
            layVerify: 'required',//必填项
            //layVerType: 'tips',//提示类型 同layui
            tips: '请选择工序...',
            data: [],
        };

        var user = xmSelect.render(userOption)

        setTimeout(function(){
            $.ajax({
                url: url + '/sys-user/userList',
                method: 'POST',
                async : false,
                contentType: 'application/json',
                data: JSON.stringify({
                    userId:"",
                    deptId:orderDept
                }),
                success: function(res) {
                    //data是ajax 异步获取的
                    var data =[];
                    for (var i = 0; i < res.data.length; i++) {
                        data.push({
                            name : res.data[i].userName,
                            value :res.data[i].loginName
                        });
                    }
                    //模拟通过ajax 获取json数据,异步更新多选下拉框的值
                    user.update({data: data})
                }
            })
        },100);
        form=layui.form;
        form.render('select');
    })

    form.on('submit(redirectBtn)', function(data) {
        console.log("校验通过，准备提交");
        var data = form.val("redirect");
        data.id=$('#orderId').val();
        data.goalProcess=$('#goalProcess').val();
        data.nowWorker = xmSelect.get('#nowWorker', true).getValue('nameStr');  //获取选中的name并用 "," 拼接。
        data.workerNo = xmSelect.get('#nowWorker', true).getValue('valueStr');  //获取选中的value并用 "," 拼接。
        console.log(data);
        $.ajax({
            url: url + '/work-order/updateOfReWorker',
            method: 'POST',
            contentType: 'application/json',
            async: false,
            data: JSON.stringify(data),
            success: function(res) {
                console.log(res);
                if (res.code == 200) {
                    parent.layer.alert('保存编辑', {
                        end: function(){
                            location.reload();
                        }
                    });
                }
            }
        })
        return false;
    })

    form.on('submit(changeBtn)', function(data) {
        layer.alert("子工单禁止修改！<br /> 有需求请联系管理员!", {
            icon: 0,
            skin: 'layer-ext-demo' //见：扩展说明
        })
        // console.log("校验通过，准备提交");
        // var data = form.val("change");
        // console.log(data);
        // $.ajax({
        //     url: url + '/work-order/update',
        //     method: 'POST',
        //     contentType: 'application/json',
        //     async: false,
        //     data: JSON.stringify(data),
        //     success: function(res) {
        //         console.log(res);
        //         if (res.code == 200) {
        //             parent.layer.alert('保存编辑', {
        //                 end: function(){
        //                     location.reload();
        //                 }
        //             });
        //         }
        //     }
        // })
        return false;
    })
}


function listInfo(index) {
    if (!index) return false;
    var orderNum;
    layer.open({
        type: 1,
        skin: 'layui-layer-rim',
        area: ['770px', '750px'],
        title: '工单详情信息',
        content: `
             <div class="layui-row layui-col-space10">
              <div style=" margin: 10px"></div>
                <div>
                <div style="float: left;margin-bottom: 5px">
                <label class="layui-form-label">工单LotNo：</label>
                <div class="layui-input-inline " style="width: 15em"  >
                <input id="orderNumOfInfo" type="text" name="orderNumOfInfo"  class="layui-input" style="width: 15em !important;" readonly="readonly">
                </div></div>
                <div style="float: right;margin-right: 50px;margin-bottom: 5px">
                <label class="layui-form-label">父工单编号：</label>
                <div class="layui-input-inline " style="width: 15em"  >
                <input id="parentLotNo" type="text" name="parentLotNo"  class="layui-input" style="width: 14.5em !important;" readonly="readonly">
                </div></div>
                </div>
                <div class="layui-col-md6">

                <label class="layui-form-label">产品类型：</label>
                <div class="layui-input-inline " style="width: 15em"  >
                <input id="productTypeOfInfo" type="text" name="productTypeOfInfo"  class="layui-input" style="width: 18em !important;" readonly="readonly">
                </div>

                <label class="layui-form-label" style="margin: 10px 0;">物料编码：</label>
                <div class="layui-input-inline " style="width: 15em">
                <input id="materialCode" type="text" name="materialCode"   class="layui-input" style="margin: 10px 0; width: 18em !important;" readonly="readonly"></div>

                <label class="layui-form-label" style="color: #FD482C">●物料名称：</label>
                <div class="layui-input-inline " style="width: 15em">
                <input id="materialName" type="text" name="materialName"   class="layui-input" style="margin-bottom: 10px;width: 18em !important;" readonly="readonly"></div>

                <label class="layui-form-label">下达车间：</label>
                <div class="layui-input-inline " style="width: 15em">
                <input id="workshopOfInfo" type="text" name="workshopOfInfo"   class="layui-input" style="width: 18em !important;" readonly="readonly"></div>
                </div>

                <div class="layui-col-md6">
                <label class="layui-form-label">单位：</label>
                <div class="layui-input-inline " style="width: 15em">
                <input id="unit" type="text" name="unit"  class="layui-input" style="width: 18em !important;" readonly="readonly"></div>
                
                <label class="layui-form-label" style="color: #FD482C">●计划数量/ACCEPT：</label>
                <div class="layui-input-inline " style="width: 10em">
                <input id="amountPlan" type="text" name="amountPlan"   class="layui-input" style="margin-bottom: 10px;width: 10em !important;" readonly="readonly"></div>
                
                <label class="layui-form-label" style="color: #FD482C">●结算数(含余数)：</label>
                <div class="layui-input-inline " style="width: 15em">
                <input id="amount" type="text" name="amount"   class="layui-input" style="margin-bottom: 10px;width: 9.5em !important;" readonly="readonly"></div>
                
                <label class="layui-form-label" style="color: #FD482C">●结算不良数：</label>
                <div class="layui-input-inline " style="width: 15em">
                <input id="badAmount" type="text" name="badAmount"   class="layui-input" style="margin-bottom: 10px;width: 11.5em !important;" readonly="readonly"></div>
                </div>

                <div>
                <div style="float: left;margin-bottom: 5px">
                <label class="layui-form-label">物料型号规格：</label>
                <div class="layui-input-inline " style="width: 15em">
                <input id="materialModel" type="text" name="materialModel"  class="layui-input" style=" width: 15em !important;" readonly="readonly"></div></div>
                <div style="float: right;margin-bottom: 5px">
                <label class="layui-form-label">工单创建人：</label>
                <div class="layui-input-inline " style="width: 15em">
                <input id="createByOfInfo" type="text" name="createByOfInfo"   class="layui-input" style="width: 10em !important;" readonly="readonly"></div>
                </div>
                </div>

                <div class="layui-row">
                <div class="layui-col-md8"><label class="layui-form-label" style="color: #FD482C">●所需工序：</label>
                <div class="layui-input-inline " style="width: 15em">
                <input id="process" type="text" name="process"   class="layui-input" style="width: 30em !important;" readonly="readonly"></div></div>
                <div class="layui-col-md4">
                <button id="workerView" style="color:#1E9FFF;" class="layui-btn layui-btn-primary layui-border-blue" lay-submit="" lay-filter="workerView">
                <b><i class="layui-icon">&#xe613;</i>   人员明细</b></button>
                </div>
                </div>

                <div class="layui-row">
                <div class="layui-col-md8"><label class="layui-form-label" style="color: #FD482C">●已经完成的工序：</label>
                <div class="layui-input-inline " style="width: 15em">
                <input id="overProcess" type="text" name="overProcess"   class="layui-input" style="width: 27em !important;" readonly="readonly"></div></div>
                <div class="layui-col-md4">
                <button style="color:#1E9FFF;" id="view" type="submit" class="layui-btn layui-btn-primary layui-border-blue" lay-submit="" lay-filter="viewReport">
                <b><i class="layui-icon">&#xe63c;</i>   查看上报记录</b></button></div></div>

                <div class="layui-row">
                <div class="layui-col-md4"><label class="layui-form-label" style="color: #FD482C">●当前工序：</label>
                <div class="layui-input-inline " style="width: 10em">
                <input id="nowProcess" type="text" name="nowProcess"   class="layui-input" style="width: 10em !important;" readonly="readonly"></div></div>
                <div class="layui-col-md4"> <label class="layui-form-label" style="float: left;color: #FD482C;" >●此工序已报：</label>
                <input id="reportedAmount" type="text" name="reportedAmount"   class="layui-input" style="width: 7em !important;" readonly="readonly"></div>
                <div class="layui-col-md4"> <label class="layui-form-label" style="float: left;color: #FD482C;" >●此工序不良：</label>
                <input id="reportedBad" type="text" name="reportedBad"   class="layui-input" style="width: 7em !important;" readonly="readonly"></div>
                </div>

                <div class="layui-row">
                <div class="layui-col-md6"><label class="layui-form-label" style="color: #FD482C">●当前工序指派人员：</label>
                <input id="nowWorker" type="text" name="nowWorker"   class="layui-input" style="width: 13em !important;" readonly="readonly"></div>
                <div class="layui-col-md6"> <label class="layui-form-label" style="float: left;color: #FD482C;" >●指派人员工号：</label>
                <input id="workerNoOfInfo" type="text" name="workerNoOfInfo"   class="layui-input" style="width: 13em !important;" readonly="readonly"></div>
                </div>
                
                <div class="layui-row">
                <div class="layui-col-md6"><label class="layui-form-label" style="color: #FD482C">●检录机台：</label>
                <input id="checkMachine" type="text" name="checkMachine"   class="layui-input" style="width: 13em !important;" readonly="readonly"></div>
                <div class="layui-col-md6"> <label class="layui-form-label" style="float: left;color: #FD482C;" >●CORE品名：</label>
                <input id="core" type="text" name="core"   class="layui-input" style="width: 13em !important;" readonly="readonly"></div>
                </div>

                <div class="layui-row">
                <div class="layui-col-md6"><label class="layui-form-label">上个工序：</label>
                <input id="pastProcess" type="text" name="pastProcess"   class="layui-input" style="width: 15em !important;" readonly="readonly"></div>
                <div class="layui-col-md6"><label class="layui-form-label">上个工序涉及人员：</label>
                <input id="pastWorker" type="text" name="pastWorker"   class="layui-input" style="width: 10em !important;" readonly="readonly"></div>
                </div>
                <div class="layui-row">
                <div class="layui-col-md6"><label class="layui-form-label">下个工序：</label>
                <input id="nextProcess" type="text" name="nextProcess"   class="layui-input" style="width: 15em !important;" readonly="readonly"></div>
                <div class="layui-col-md6"><label class="layui-form-label">下个工序涉及人员：</label>
                <input id="nextWorker" type="text" name="nextWorker"   class="layui-input" style="width: 10em !important;" readonly="readonly"></div>
                </div>

                <div align="center" id="buttonArea">
                <button id="submit" style="color:#1E9FFF;" type="submit" class="layui-btn layui-btn-primary layui-border-blue" lay-submit="" lay-filter="exportFile">
                <b><i class="layui-icon">&#xe67d;</i>   查看生产流程单</b></button>
                </div>
               </div>

          `,
        success: function(){
            $.ajax({
                url: url + '/work-order/orderInfo/'+index,
                contentType: 'application/json',
                async: false,
                success: function(res) {
                    // form.val("data", res.data);
                    $('#orderNumOfInfo').val(res.data.orderNum);
                    $('#productTypeOfInfo').val(res.data.productType);
                    $('#materialCode').val(res.data.materialCode);
                    $('#materialName').val(res.data.materialName);
                    $('#workshopOfInfo').val(res.data.workshop);
                    $('#unit').val(res.data.unit);
                    $('#amount').val(res.data.amount);
                    $('#badAmount').val(res.data.badAmount);
                    $('#amountPlan').val(res.data.amountPlan);
                    $('#createByOfInfo').val(res.data.createBy);
                    $('#materialModel').val(res.data.materialModel);
                    $('#nowWorker').val(res.data.nowWorker);
                    $('#workerNoOfInfo').val(res.data.workerNo);
                    $('#pastWorker').val(res.data.pastWorker);
                    $('#nextWorker').val(res.data.nextWorker);
                    $('#parentLotNo').val(res.data.lotNo);
                    $('#checkMachine').val(res.data.checkMachine);
                    $('#core').val(res.data.corePin);
                    form.render();
                    //获取已报数据
                    $.ajax({
                        url: url + '/report-order/listNumOfReported',
                        method: 'POST',
                        contentType: 'application/json',
                        async: false,
                        data: JSON.stringify({
                            orderNum: res.data.orderNum,
                            reportProcess: res.data.nowProcess,
                            workerNo: res.data.workerNo,
                        }),
                        success: function(res3) {
                            $('#reportedAmount').val(res3.data.amount);
                            $('#reportedBad').val(res3.data.badAmount);
                        }
                    })
                    orderNum=res.data.orderNum;
                }
            })
            //获取工序
            $.ajax({
                url: url + '/work-order/getProcess/'+index,
                contentType: 'application/json',
                async: false,
                success: function(res) {
                    //0是所需工序；1是已经完成的工序；2是当前工序；3是上个工序；4是下个工序
                    $('#process').val(res.data[0]);
                    $('#overProcess').val(res.data[1]);
                    $('#nowProcess').val(res.data[2]);
                    $('#pastProcess').val(res.data[3]);
                    $('#nextProcess').val(res.data[4]);
                }
            })
        }
    });

    form.on('submit(workerView)', function() {
        $("#viewWorker").empty();
        var processList=[];
        var workerList=[];
        var actualList=[];
        $.ajax({
            url: url + '/work-order/getWorkerInfo/'+orderNum.replace("#","%23"),
            contentType: 'application/json',
            async: false,
            success: function(res) {
                //获得数据
                processList=res.data.processList
                workerList=res.data.workerList
            }
        })
        $.ajax({
            url: url + '/report-order/actualWorker/'+orderNum.replace("#","%23"),
            contentType: 'application/json',
            async: false,
            success: function(res) {
                //获得数据
                actualList=res.data
            }
        })
        var trs='';
        trs+='<div style="margin: 5px" align="center"><h3><b> 委派分工明细 </b></h3></div>'
        trs+='<div align="center"><table style="margin: 10px" border="2" width="510px" height="80px">'
        trs+='<tr>'
        for(var i=0;i<processList.length;i++){
            trs+='<td><b style="color: #F5222D">'+processList[i]+'</b></td>'
        }
        trs+='</tr>'
        trs+='<tr>'
        for(var j=0;j<workerList.length;j++){
            trs+='<td><b>'+workerList[j]+'</b></td>'
        }
        trs+='</tr>'
        trs+='</table></div>';

        trs+='<div style="margin-top: 30px" align="center"><h3><b> 实际执行人明细 </b></h3></div>'
        trs+='<div align="center"><table style="margin: 10px" border="2" width="510px" height="80px">'
        trs+='<tr>'
        for(var i=0;i<processList.length;i++){
            trs+='<td><b style="color: #F5222D">'+processList[i]+'</b></td>'
        }
        trs+='</tr>'
        trs+='<tr>'
        for(var j=0;j<actualList.length;j++){
            trs+='<td><b>'+actualList[j]+'</b></td>'
        }
        trs+='</tr>'
        trs+='</table></div>';
        trs+='<div align="center" style="margin: 10px"><button class="layui-btn layui-btn-danger" onclick="layer.close(layer.index)"><b><i class="layui-icon">&#xe605;</i></b></button></div>';

        $("#viewWorker").append(trs);
        layer.open({
            type: 1,
            title: '人员分工预览',
            maxmin: true,
            shadeClose: true, //点击遮罩关闭层
            area : ['600px' , '450px'],
            content: $('#viewWorker'),
            end:function () {
                $('#viewWorker').css('display','none')
            }
        });
    })

    form.on('submit(exportFile)', function() {
        $("#processPdf").empty();
        $.ajax({
            url: url + '/work-order/getFlow/'+orderNum,
            method: 'GET',
            contentType: 'application/json',
            // data: JSON.stringify({
            //     orderNum: data
            // }),
            success: function (res) {
                var trs='';
                trs+='<div>' +
                    '<button class="layui-btn layui-btn-danger" id="downloadPDF"><i class="layui-icon">&#xe67d;导出流程单</i>    </button>' +
                    '<span>(提示：建议使用横向打印;如遇表格线条缺失,更换横向打印可以解决;若不能解决,建议使用谷歌内核或者Edge浏览器)</span>' +
                    '</div>';
                trs+='<div id="PDFPage" align="center" style="background-color: #d5e7ea" >'
                trs+='<div style="margin: 5px" align="center"><h3><b> 生产流程单 </b></h3></div>'
                trs+='<div align="center" style="width: 865px">'
                trs+='<div style="width: auto;">  ' +
                    '    <div style="float: left;width: 30%;position: relative;">' +
                    '    <span style="float: left">品名/PIN:<b>'+res.data.pinName+'</b></span>' +
                    '    </div>' +
                    '    <div style="float: left;width: 30%;position: relative;">' +
                    '    <span style="float: right">工单号/LotNo:<b>'+res.data.orderNum+'</b></span>' +
                    '    </div>' +
                    '    <div style="float: right;width: 30%;position: relative;">' +
                    '    <span style="float: right">创建日期:<b>'+res.data.createTime+'</b></span>' +
                    '    </div>' +
                    ' </div>'
                //开始生成表格
                trs+='<table style="width: 865px;" border="2">'
                //生成表头
                trs+=' <tr>'+
                    '  <th style="height: 65px">流程名</th>'+
                    '  <th style="height: 65px;width: 400px">制程条件</th>'+
                    '  <th style="height: 65px">接收数量</th>'+
                    '  <th style="height: 65px">不良数</th>'+
                    '  <th style="height: 65px">良品数</th>'+
                    '  <th style="height: 65px">执行人</th>'+
                    '  <th style="height: 65px">上报人</th>'+
                    '  <th style="height: 65px">完结日期</th>'
                trs+=' </tr>'
                for(var i=0;i<res.data.processNames.length;i++){
                    trs+=' <tr>'+
                        '  <td style="height: 85px;text-align: center;vertical-align: center">'+res.data.processNames[i]+'</td>'+
                        '  <td style="height: 85px;text-align: left;vertical-align: top">' +
                        '涉及机台:'+res.data.machineMessages[i]+'' +
                        '</td>'+
                        '  <td style="height: 85px;text-align: center;vertical-align: center">'+res.data.amountPlans[i]+'</td>'+
                        '  <td style="height: 85px;text-align: center;vertical-align: center">'+res.data.badAmounts[i]+'</td>'+
                        '  <td style="height: 85px;text-align: center;vertical-align: center">'+res.data.amounts[i]+'</td>'+
                        '  <td style="height: 85px;text-align: center;vertical-align: center">'+res.data.workers[i]+'</td>'+
                        '  <td style="height: 85px;text-align: center;vertical-align: center">'+res.data.reporters[i]+'</td>'+
                        '  <td style="height: 85px;text-align: center;vertical-align: center">'+res.data.reportTimes[i]+'</td>'
                    trs+=' </tr>'
                }
                //生产流程单固定样式生成
                for(var j=0;j<2;j++){
                    trs+=' <tr>'
                    if(j==0){
                        trs+='  <td style="height: 55px;text-align: center;vertical-align: center">FQC</td>' +
                            '<td style="height: 55px">合格<i class="layui-icon">&#xe63f;</i>&nbsp;&nbsp;&nbsp;&nbsp;不合格<i class="layui-icon">&#xe63f;</i></td>'
                    }
                    if(j==1){
                        trs+='  <td style="height: 55px;text-align: center;vertical-align: center">预定交期</td>' +
                            ' <td style="height: 55px"></td>'
                    }
                    trs+=' <td style="height: 55px"></td>'+
                        '  <td style="height: 55px"></td>'+
                        '  <td style="height: 55px"></td>'+
                        '  <td style="height: 55px"></td>'+
                        '  <td style="height: 55px"></td>'+
                        '  <td style="height: 55px"></td>'
                    trs+=' </tr>'
                }
                trs+='</table>'
                trs+='</div></div>'
                $("#processPdf").append(trs);
                layer.open({
                    type: 1,
                    title: '生产流程单',
                    maxmin: true,
                    shadeClose: true, //点击遮罩关闭层
                    area : ['1050px' , '720px'],
                    content: $('#processPdf'),
                    end:function () {
                        $('#processPdf').css('display','none')
                    }
                });
                //下载方法
                $('#downloadPDF').click(function () {
                    let confirm1=layer.confirm('准备导出流程单PDF', {
                            btn: ['确认','取消']
                        },function () {
                            $("#PDFPage").jqprint({
                                debug: false,//如果是true则可以显示iframe查看效果（iframe默认高和宽都很小，可以再源码中调大），默认是false
                                importCSS: true, //true表示引进原来的页面的css，默认是true。（如果是true，先会找$("link[media=print]")，若没有会去找$("link")中的css文件）
                                printContainer: true,//表示如果原来选择的对象必须被纳入打印（注意：设置为false可能会打破你的CSS规则）
                                operaSupport: false//表示如果插件也必须支持歌opera浏览器，在这种情况下，它提供了建立一个临时的打印选项卡。默认是true
                            });
                            layer.close(confirm1);
                        }
                        ,function (){
                            layer.close(confirm1);
                        }
                    )
                })
            }
        })
    })



    form.on('submit(viewReport)', function(data) {
        var data = orderNum;
        $.ajax({
            url: url + '/report-order/listReport',
            method: 'POST',
            contentType: 'application/json',
            data: JSON.stringify({
                orderNum:data
            }),
            success: function (res) {
                if(res.code==400){
                    layer.msg(res.msg);
                    return;
                }
                $("#viewDiv").empty();
                var trs='';
                var amount=0;
                var amountPlan=0;
                var badTotalAmount=0;
                var missingAmount=0;
                //获取数量
                $.ajax({
                    url: url + '/report-order/listNum/'+data.replace("#","%23"),
                    contentType: 'application/json',
                    async: false,
                    success: function(res) {
                        amount=res.data.amount;
                        amountPlan=res.data.amountPlan;
                        badTotalAmount=res.data.badTotalAmount;
                        missingAmount=amountPlan-amount-badTotalAmount;
                    }
                })
                trs+='<div style="margin: 5px" align="center"><h3><b> 最终数据总计 </b></h3></div>'
                trs+='<div align="center"><table style="margin: 10px" border="2" width="510px" height="80px">' +
                    '    <tr>' +
                    '        <td><b>计划数量</b></td>' +
                    '        <td><b>结算数量(含余数)</b></td>' +
                    '        <td><b>总不良品数</b></td>' +
                    '        <td><b>总缺数</b></td>' +
                    '    </tr>' +
                    '    <tr>' +
                    '        <td><b style="color: #1890ff">'+amountPlan+'</b></td>' +
                    '        <td><b style="color: #2b8d08">'+amount+'</b></td>' +
                    '        <td><b style="color: #ff6918">'+badTotalAmount+'</b></td>' +
                    '        <td><b style="color: #ff1818">'+missingAmount+'</b></td>' +
                    '    </tr>' +
                    '</table></div>';
                for (var i = 0; i < res.data.length; i++) {
                    var j=i+1;
                    trs += '<div class="page-content layui-collapse">';
                    trs += '<div class="supplier_style layui-colla-item">';
                    trs += '<div class="info_Content" style="font-size: 15px">';
                    if(res.data[i].over){
                        trs +='<h2 class="layui-colla-title"><b style="color: #f5222d">'+res.data[i].reportProcess+'  </b><b>上报记录</b></h2>';
                    }else {
                        trs +='<h2 class="layui-colla-title"><b style="color: #f5222d">'+res.data[i].reportProcess+'  </b><b>上报记录  (未完工,待结算)</b></h2>';
                    }
                    trs +='<div class="layui-colla-content layui-show">';
                    trs += '<ul class="Content_list clearfix">';
                    // trs += '<label class="label_name" style="line-height: 2"><b style="color: #f5222d">——上报记录'+j+':             </b></label>';
                    trs += '<li style="line-height:2.5"><label class="label_name"><b>●  上报工单的工单号:             </b></label><span style="color: #1890ff">'+res.data[0].orderNum+'</span></li>';
                    trs += '<li style="line-height:2.5"><label class="label_name"><b>●  上报人名字:                  </b></label><span style="color: #1890ff">'+res.data[i].reporter+'</span></li>';
                    trs += '<li style="line-height:2.5"><label class="label_name"><b>●  上报的工序:                  </b></label><span style="color: #1890ff">'+res.data[i].reportProcess+'</span></li>';
                    trs += '<li style="line-height:2.5"><label class="label_name"><b>●  上报的良品数量(含余数录入):             </b></label><span style="color: #1890ff">'+res.data[i].reportAmount+'</span></li>';
                    trs += '<li style="line-height:2.5"><label class="label_name"><b>●  上报的不良品数量:                      </b></label><span style="color: #1890ff">'+res.data[i].badAmount+'</span></li>';
                    trs += '<li style="line-height:2.5"><label class="label_name"><b>●  余数录入:                      </b></label><span style="color: #1890ff">'+res.data[i].supplyAmount+'</span></li>';
                    trs += '<li style="line-height:2.5"><label class="label_name"><b>●  作业机台:                      </b></label><span style="color: #1890ff">'+res.data[i].machineMessage+'</span></li>';
                    trs += '<li style="line-height:2.5"><label class="label_name"><b>●  该工序执行人:                 </b></label><span style="color: #1890ff">'+res.data[i].processWorker+'</span></li>';
                    trs += '<li style="line-height:2.5"><label class="label_name"><b>●  上报时间:                    </b></label><span style="color: #1890ff">'+res.data[i].createTime+'</span></li>';
                    trs += '</ul>';
                    trs += '</div>';
                    trs += '</div>';
                    trs += '</div>';
                    trs += '</div>';
                }
                trs+='<div align="center" style="margin: 10px"><button class="layui-btn layui-btn-danger" onclick="layer.close(layer.index)"><b><i class="layui-icon">&#xe605;</i></b></button></div>';
                $("#viewDiv").append(trs);
                layer.open({
                    type: 1,
                    title: '上报记录详情',
                    maxmin: true,
                    shadeClose: true, //点击遮罩关闭层
                    area : ['600px' , '700px'],
                    content: $('#viewDiv'),
                    end:function () {
                        $('#viewDiv').css('display','none')
                    }
                });
            }
        })
    });
}


function deleteOrder(index) {
    layer.alert("子工单禁止删除！<br /> 有需求请联系管理员!", {
        icon: 0,
        skin: 'layer-ext-demo' //见：扩展说明
    })
    // if (!index) return false;
    // let confirm1=layer.confirm('您确定要删除这条工单吗？', {
    //         btn: ['确定','取消']
    //     },function () {
    //         $.ajax({
    //             url: url + '/work-order/delete',
    //             method: 'POST',
    //             contentType: 'application/json',
    //             data: JSON.stringify({
    //                 userId: sessionStorage.getItem('userId'),
    //                 goalId: index
    //             }),
    //             success: function (res) {
    //                 layer.msg(res.msg)
    //                 if (res.code == 200) search();
    //             }
    //         })
    //         layer.close(confirm1);
    //     }
    //     ,function (){
    //         layer.close(confirm1);
    //     }
    // )
}

function resizeLayer(layerIndex, layerInitWidth, layerInitHeight) {
    var windowWidth = $(document).width();
    var windowHeight = $(document).height();
    var minWidth = layerInitWidth > windowWidth ? windowWidth : layerInitWidth;
    var minHeight = layerInitHeight > windowHeight ? windowHeight : layerInitHeight;
    console.log( "win:" , windowWidth, windowHeight);
    console.log( "lay:" , layerInitWidth, layerInitHeight);
    console.log( "min:" , minWidth, minHeight);
    layer.style(layerIndex, {
        top: 0,
        width: minWidth,
        height: minHeight
    });
}

function reportOrder(index) {
    $.ajax({
        url: url + '/work-order/checkUser',
        method: 'POST',
        contentType: 'application/json',
        async: false,
        data: JSON.stringify({
            userId:sessionStorage.getItem('userId'),
            orderId:index
        }),
        success: function(res) {
            if (res.code == 400) {
                layer.msg(res.msg)
            }else {
                $.ajax({
                    url: url + '/work-order/isOver/'+index,
                    method: 'GET',
                    contentType: 'application/json',
                    async: false,
                    success: function(res) {
                        if (res.code == 400) {
                            layer.msg(res.msg)
                        }else{
                            $.ajax({
                                url: url + '/work-order/isCheck/'+index,
                                method: 'GET',
                                contentType: 'application/json',
                                async: false,
                                success: function(res2) {
                                    if(res2.code==400){
                                        layer.msg(res2.msg)
                                    }else {
                                        layer.open({
                                            type: 1,
                                            skin: 'layui-layer-rim',
                                            area: ['435px', '735px'],
                                            title: '上报工序',
                                            content: `
            <form class="layui-form" action="" lay-filter="report">
              <div class="layui-row" style="margin-bottom: 10px"></div>

                <input id="orderId" type="text" name="id" style="display: none;">

                <label class="layui-form-label">上报的工单号：</label>
                <div class="layui-input-inline " style="width: 15em"  >
                <input id="orderNumOfReport" type="text" name="orderNumOfReport" placeholder="输入工单号" lay-verify="required" class="layui-input" style="margin-bottom: 10px ; width: 16em !important;" readonly="readonly">
                </div>

                <label class="layui-form-label"><b style="color: #f5222d">*</b>上报工序：</label>
                <div class="layui-input-inline " style="width: 15em"  >
                <input id="nowProcess" type="text" name="nowProcess" placeholder="输入当前工序" lay-verify="required" class="layui-input" style="margin-bottom: 10px ;width: 18em !important;" readonly="readonly">
                </div>

                <div style="display: none"><label class="layui-form-label">工作中心：</label>
                <div class="layui-input-inline " style="width: 15em">
                <input id="workCenter" type="text" readonly="readonly" name="workCenter" placeholder="输入工作中心" class="layui-input" style="margin-bottom: 10px;width: 18em !important;"></div>
                </div>
                
                <div class="layui-row">
                   <div class="layui-col-md6">
                     <label class="layui-form-label">计划接收：</label>
                     <input id="amountPlan" type="text" name="amountPlan" placeholder="输入计划接收数量" readonly="readonly" lay-verify="required" class="layui-input" style="margin-bottom: 10px;width: 6em !important;">
                   </div>
                    <div class="layui-col-md6">
                      <label class="layui-form-label">上游结算：</label>
                      <input id="nowAmount" type="text" name="nowAmount" placeholder="上游结算数量" readonly="readonly" lay-verify="required" class="layui-input" style="margin-bottom: 10px;width: 6em !important;">
                    </div>
                </div>
                
                <div class="layui-row">
                  <div class="layui-col-md6">
                    <label class="layui-form-label">已报数量：</label>
                    <input id="reportedAmount" type="text" name="reportedAmount" placeholder="此工序已报数量" readonly="readonly" lay-verify="required" class="layui-input" style="margin-bottom: 10px;width: 6em !important;">
                  </div>
                  <div class="layui-col-md6">
                    <label class="layui-form-label">已报不良：</label>
                    <input id="reportedBad" type="text" name="reportedBad" placeholder="此工序已报不良" readonly="readonly" lay-verify="required" class="layui-input" style="margin-bottom: 10px;width: 6em !important;">
                  </div>
                </div>
                <input id="reportedSupply" type="text" name="reportedSupply" placeholder="此工序补录数量" readonly="readonly" lay-verify="required" class="layui-input" style="display: none;margin-bottom: 10px;width: 6em !important;">

                
                <label class="layui-form-label">终检余数录入：</label>
                <div class="layui-input-inline " style="width: 15em">
                <input id="supplyAmount" onkeyup="value=value.replace(/[^\\d]/g,'')" type="text" name="supplyAmount" placeholder="录入余数(终检专用,其他站无效)" class="layui-input" style="margin-bottom: 10px;width: 15.8em !important;"></div>
                
                <label class="layui-form-label"><b style="color: #f5222d">*</b>上报(良品)数量：</label>
                <div class="layui-input-inline " style="width: 15em">
                <input id="reportAmount" onkeyup="value=value.replace(/[^\\d]/g,'')" type="text" name="reportAmount" placeholder="输入上报数量" lay-verify="required" class="layui-input" style="margin-bottom: 10px;width: 14.9em !important;"></div>

                <label class="layui-form-label"><b style="color: #f5222d">*</b>不良数量：</label>
                <div class="layui-input-inline " style="width: 15em">
                <input id="badAmount" onkeyup="value=value.replace(/[^\\d]/g,'')"  type="text" name="badAmount" placeholder="输入不良数量" lay-verify="required" class="layui-input" style="margin-bottom: 10px;width: 17.5em !important;"></div>
                
                <label class="layui-form-label"><b style="color: #f5222d">*</b>工序执行人：</label>
                <div class="layui-input-inline " style="width: 16.5em">
                <select name="processWorker" id="processWorker"  lay-filter="processWorker" lay-verify="required" lay-search=""></select></div>

                <label class="layui-form-label" style="margin: 10px"><b style="color: #f5222d">*</b>上报人：</label>
                <div class="layui-input-inline " style="width: 15em">
                <input id="reporter" type="text" name="reporter" placeholder="输入上报人" lay-verify="required" class="layui-input"  style="margin: 10px 0; width: 17em !important;"></div>
                
                <div class="layui-input-inline" style="margin: 2px;"><label class="layui-form-label">CORE批号：</label>
                <div class="layui-input-inline " style="width: auto">
                <input id="coreNum" type="text" name="coreNum" placeholder="输入CORE批号(第一站必填)" class="layui-input"  style="margin-bottom: 10px;width: 16.5em !important;">
                </div></div>
                
                <div class="layui-input-inline" style="margin: 3px;"><label class="layui-form-label">材料批号：</label>
                <div class="layui-input-inline " style="width: 15em">
                <input id="stuffNum" type="text" name="stuffNum" placeholder="输入材料批号(耗材工序必填)" class="layui-input"  style="margin-bottom: 10px;width: 17em !important;">
                </div></div>
               
                <label class="layui-form-label" style="margin: 3px">作业机台：</label>
                <div class="layui-input-inline " style="width: 15em">
                <input id="machineNum" type="text" name="machineNum" placeholder="输入作业机台" class="layui-input"  style="margin-bottom: 10px;width: 17em !important;">
                </div>
                
                <div class="layui-input-inline" style="margin: 3px;">
                <label class="layui-form-label">工序状态：</label>
                <input type="radio" name="overStatus" value="1" title="完结工序" checked="">
                <input type="radio" name="overStatus" value="0" title="未完结工序" >
                <b><i id="help" class="layui-icon" style="font-size: 25px;">&#xe60b;</i></b>
                </div>

                <div align="center" style="margin: 15px">
                <button id="submit" type="submit" class="layui-btn" lay-submit="" lay-filter="reportBtn" >完成上报</button>
                <button type="button" class="layui-btn layui-btn-danger" onclick="layer.close(layer.index)">取消上报</button></div>

            </form>
          `,
                                            success: function(){
                                                //获取当前弹出窗口的索引及初始大小
                                                var isHis=0;//标识符,如果是多人协作工单，用于判断当前用户是否是其中一员
                                                var allWorkerNo=[];
                                                $.ajax({
                                                    url: url + '/work-order/getOrderById/'+index,
                                                    contentType: 'application/json',
                                                    async: false,
                                                    success: function(res) {
                                                        //得到委派人
                                                        allWorkerNo=res.data.workerNo.split(",");
                                                        for(var i=0;i<allWorkerNo.length;i++){
                                                            if(allWorkerNo[i]==sessionStorage.getItem("loginName")){//如果委派人有当前用户
                                                                isHis=1;
                                                            }
                                                        }
                                                        // form.val("data", res.data);
                                                        //获取基础数据
                                                        $('#orderId').val(res.data.id);
                                                        $('#orderNumOfReport').val(res.data.orderNum);
                                                        $('#nowProcess').val(res.data.nowProcess);
                                                        $('#amountPlan').val(res.data.amountPlan);
                                                        //获取机台
                                                        // $.ajax({
                                                        //     url: url + '/all-machine/listMachineNum',
                                                        //     method: 'POST',
                                                        //     async : false,
                                                        //     contentType: 'application/json',
                                                        //     data: JSON.stringify({
                                                        //         userId:"",
                                                        //         deptId: res.data.orderDept
                                                        //     }),
                                                        //     success: function(res2) {
                                                        //         if (res2.code == 200) {
                                                        //             for (var i = 0; i < res2.data.length; i++) {
                                                        //                 $("#machineId").append("<option value='" + res2.data[i].id + "'>" + res2.data[i].machineInfoName+'   '+res2.data[i].machineNum+ "</option>");
                                                        //             }
                                                        //             form.render('select');
                                                        //         }
                                                        //     }
                                                        // })
                                                        //获取已报数据
                                                        $.ajax({
                                                            url: url + '/report-order/listNumOfReported',
                                                            method: 'POST',
                                                            contentType: 'application/json',
                                                            async: false,
                                                            data: JSON.stringify({
                                                                orderNum: res.data.orderNum,
                                                                //reportProcess: res.data.nowProcess,
                                                                workerNo: res.data.workerNo,
                                                            }),
                                                            success: function(res3) {
                                                                $('#nowAmount').val(res3.data.nowAmount);
                                                                $('#reportedAmount').val(res3.data.amount);
                                                                $('#reportedBad').val(res3.data.badAmount);
                                                                $('#reportedSupply').val(res3.data.reportedSupply);
                                                            }
                                                        })
                                                        //获取指派用户下拉框
                                                        $.ajax({
                                                            url: url + '/sys-user/userList',
                                                            method: 'POST',
                                                            async : false,
                                                            contentType: 'application/json',
                                                            data: JSON.stringify({
                                                                userId:"",
                                                                deptId: res.data.orderDept
                                                            }),
                                                            success: function(res4) {
                                                                if (res4.code == 200) {
                                                                    for (var i = 0; i < res4.data.length; i++) {
                                                                        //上报人填入
                                                                        if(res4.data[i].loginName===sessionStorage.getItem("loginName")){
                                                                            $("#reporter").val(res4.data[i].userName)
                                                                        }
                                                                        //多人执行工序时，只默认选中自己
                                                                        // if(isHis==1){
                                                                        //     if(res4.data[i].loginName==sessionStorage.getItem("loginName")){
                                                                        //         $("#processWorker").append("<option selected='selected' value='" + res4.data[i].loginName + "'>" + res4.data[i].loginName+" "+res4.data[i].userName + "</option>");
                                                                        //     }else
                                                                        //         $("#processWorker").append("<option value='" + res4.data[i].loginName + "'>"+ res4.data[i].loginName+" " + res4.data[i].userName + "</option>");
                                                                        // }else if(isHis==0){
                                                                        //     if(res4.data[i].loginName==allWorkerNo[0]){
                                                                        //         $("#processWorker").append("<option selected='selected' value='" + res4.data[i].loginName + "'>"+ res4.data[i].loginName+" "+ res4.data[i].userName + "</option>");
                                                                        //     }else
                                                                        //         $("#processWorker").append("<option value='" + res4.data[i].loginName + "'>"+ res4.data[i].loginName+" "+ res4.data[i].userName + "</option>");
                                                                        // }
                                                                        if(res4.data[i].loginName==sessionStorage.getItem("loginName")){
                                                                            $("#processWorker").append("<option selected='selected' value='" + res4.data[i].loginName + "'>" + res4.data[i].loginName+" "+res4.data[i].userName + "</option>");
                                                                        }else
                                                                            $("#processWorker").append("<option value='" + res4.data[i].loginName + "'>"+ res4.data[i].loginName+" " + res4.data[i].userName + "</option>");

                                                                        //已指派人员默认选择
                                                                    }
                                                                    form.render('select');
                                                                }
                                                            }
                                                        })
                                                        form.render();
                                                    }
                                                })
                                            }
                                        });
                                    }
                                }
                            })
                        }
                    }
                })
            }
        }
    })


    $(function(){
        var tips;
        $('#what').on({
            mouseenter:function(){
                var that = this;
                tips = layer.tips('', '#what', {
                    tips: [1, '#3595CC'],
                    time: 0
                });
            },
            mouseleave:function(){
                layer.close(tips);
            }
        });
    })


    $(function(){
        var tips;
        $('#help').on({
            mouseenter:function(){
                var that = this;
                tips = layer.tips('选择未完结的情况下可以二次上报或多次上报;选择完结则表示该工单的此道工序彻底完结', '#help', {
                    tips: [1, '#3595CC'],
                    time: 0
                });
            },
            mouseleave:function(){
                layer.close(tips);
            }
        });
    })
    //余数录入输入框联动
    $('#supplyAmount').bind('input propertychange',function () {
        var accept = Number($('#amountPlan').val());
        var lastAmount = Number($('#nowAmount').val());
        var reportedSupply=Number($('#reportedSupply').val());
        var supplyAmount;
        if($('#supplyAmount').val()==""){
            supplyAmount= 0;
        }else {
            supplyAmount = Number($('#supplyAmount').val());
        }
        //不允许超出接收数
        if(lastAmount===0){
            $('#supplyAmount').val(0)
        }else {
            if(supplyAmount+lastAmount>accept){
                $('#supplyAmount').val(accept-lastAmount-reportedSupply)
            }
        }
    });
    //上报数量输入框联动
    $('#reportAmount').bind('input propertychange',function () {
        var accept = Number($('#amountPlan').val());
        var lastAmount = Number($('#nowAmount').val());
        var reportAmount = Number($('#reportAmount').val());
        var report=Number($('#reportedAmount').val())+Number($('#reportedBad').val())-Number($('#reportedSupply').val());
        var badAmount;
        if(lastAmount===0){
            badAmount = Number(accept-report -reportAmount);
        }else {
            badAmount = Number(lastAmount-report -reportAmount);
        }
        if(badAmount<=0){
            $('#badAmount').val(0);
        }else
            $('#badAmount').val(badAmount);
    });

    form.on('submit(reportBtn)', function() {
        console.log("校验通过，准备提交");
        var data = form.val("report");
        var msg="请确认上报数据是否无误！";
        var checkResult=checkAmount(parseInt(data.amountPlan),parseInt(data.nowAmount),parseInt(data.reportedAmount),
            parseInt(data.reportedBad),parseInt(data.reportAmount),parseInt(data.badAmount),parseInt(data.overStatus));
        if(checkResult==="300"){
            msg+="</br>●注意：您当前上报的总数已达到接收或上游结算数量,应当选择<b style='color:#ff0000;'>完结工序</b>!"
        }else if(checkResult==="400"){
            msg+="</br>●注意：您当前上报的总数没有达到接收或上游结算数量,应当选择<b style='color:#ff0000;'>未完结工序</b>，否则将记录缺数!"
        }
        data.orderNum=$('#orderNumOfReport').val()//工单号
        data.workerNo=$('#processWorker').val()//工号
        data.processWorker=$('#processWorker option:selected').text()//上报人姓名
        //余数默认为0
        if(data.supplyAmount==""){
            data.supplyAmount=0;
        }
        console.log(data);
        let confirm1=layer.confirm(msg, {
                btn: ['我已确定','再看看']},function(){
                $.ajax({
                    url: url + '/report-order/report',
                    method: 'POST',
                    contentType: 'application/json',
                    async: false,
                    data: JSON.stringify(data),
                    success: function(res) {
                        console.log(res);
                        if (res.code == 200) {
                            parent.layer.alert('上报成功', {
                                end: function(){
                                    location.reload();
                                }
                            });
                        }else {
                            layer.alert(res.msg, {
                                icon: 2,
                                skin: 'layer-ext-demo' //见：扩展说明
                            })
                        }
                    }
                })},function () {
                layer.close(confirm1);
            }
        )
        return false;
    });
}


function search(index,sort) {
    //加载层遮罩
    var loading = layer.load(3, {
        shade: [0.1,'#fff'] //0.1透明度的白色背景
    });
    if (!index) var index = displayStart;
    var isDisabled=false;
    var isChecked=false;
    var data = form.val('data');
    data.displayStart = index;
    data.userId= sessionStorage.getItem('userId')
    data.status=$('#status').val();
    data.pinId=$('#pinName').val();
    data.sortStatus=sort;
    $.ajax({
        url: url + '/work-order/listOrder',
        method: 'POST',
        contentType: 'application/json',
        data: JSON.stringify(data),
        success: function(res) {
            //关闭加载层
            layer.close(loading)
            //total=Math.ceil(res.data.records.length/10);
            $('tbody').empty();
            displayStart = res.data.current;
            endPage = res.data.pages;
            list = res.data.records;
            for (var i = 0; i < res.data.records.length; i++) {
                //先判断是否有模板或者是否列印过
                $.ajax({
                    url: url+'/excel/checkMould/'+res.data.records[i].id,
                    method: 'GET',
                    contentType: 'application/json',
                    async : false,
                    success: function (res) {
                        if (res.code==200){
                            isDisabled=true;
                        } else if (res.code==400){
                            isDisabled=false;
                        }
                    }
                })
                var createTime=res.data.records[i].createTime.split(".");
                $('tbody').append(
                    "<tr><td>"+checkMould(isDisabled,res.data.records[i].id)+"</td>"+
                    "<td class='createTime'>" + res.data.records[i].orderNum + "</td>" +
                    "<td class='createTime'>" + res.data.records[i].pinName + "</td>" +
                    "<td class='createTime'>" + res.data.records[i].lotNo + "</td>" +
                    "<td class='createTime'>" + createTime[0].replace('T', ' ')  + "</td>" +
                    "<td>" + res.data.records[i].createBy + "</td>" +
                    "<td>" + res.data.records[i].productType + "</td>" +
                    // "<td>" + res.data.records[i].workshop + "</td>" +
                    "<td>" + res.data.records[i].amount + "</td>" +
                    "<td>" + res.data.records[i].amountPlan + "</td>" +
                    // "<td>" + res.data.records[i].nowProcess + "</td>" +
                    // "<td>" + res.data.records[i].pastProcess + "</td>" +
                    // "<td>" + res.data.records[i].nextProcess + "</td>" +
                    // "<td>" + res.data.records[i].nowWorker+ "</td>" +
                    "<td id='statusTd' title='点击进入审核' onclick='checkOrder("+res.data.records[i].id+")'>" + isOrderStatus(res.data.records[i].orderStatus,res.data.records[i].check)+ "</td>" +
                    "<td>"+isCheckInit(res.data.records[i].check,res.data.records[i].id)+"</td>"+
                    "<td onclick='checkInIt(\"" + res.data.records[i].orderNum + "\",\"" + res.data.records[i].nowProcess + "\")'>"+checkStatus(res.data.records[i].check) +"</td>"+
                    "<td>" +
                    "<button type='button' style='background-color:#2F4056;' class='layui-btn layui-btn-sm' onclick='complementBtu(" + res.data.records[i].id +")'>校正</button>" +
                    "<button type='button' style='background-color:#305087;' class='layui-btn layui-btn-sm' onclick='reProBtn(\"" + res.data.records[i].orderNum + "\")'>返工</button>" +
                    "</td>"+
                    "<td class='operation'><button type='button' class='layui-btn layui-bg-green layui-btn-sm ' onclick='reportOrder(" + res.data.records[i].id + ")'>上报</button>" +
                    "<button type='button' class='layui-btn layui-btn-warm layui-btn-sm' onclick='listInfo(" + res.data.records[i].id +")'>详情</button>" +
                    "<button type='button' class='layui-btn layui-btn-normal layui-btn-sm' onclick='edit(" + res.data.records[i].id +")'>编辑</button>" +
                    "<button type='button' class='layui-btn layui-btn-danger layui-btn-sm ' onclick='deleteOrder(" + res.data.records[i].id + ")'>删除</button></td>" +
                    "</tr>"
                );
            }
            $('#total').html(Math.ceil(res.data.records[0].allTotal/Number($('#displayLength').val())))
            $('#page').html(displayStart)
        }
    })
}

function complementBtu(index) {
    var nowProcessId;
    let confirm1=layer.confirm('注意!</br>' +
        '报工错误校正<b style="color:#ff0000;">会删除上一站报工记录！</br>该行为会被记录！</b>', {
            btn: ['确认','取消']
        },function () {
            $.ajax({
                url: url + '/report-order/getDeleteInfo',
                method: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({
                    userId:sessionStorage.getItem('userId'),
                    orderId:index
                }),
                success: function(res) {
                    if(res.code==400){
                        layer.msg(res.msg)
                    }else {
                        layer.open({
                            type: 1,
                            skin: 'layui-layer-rim',
                            area: ['340px', '320px'],
                            title: '删除报工错误记录',
                            content: `
              <div class="layui-row" style="margin-bottom: 10px"></div>
                <input id="orderId" type="text" name="id" style="display: none;">
                
                <div class="layui-input-inline" style="margin: 5px;">
                <label class="layui-form-label">删除的工序：</label>
                <div class="layui-input-inline " style="width: 15em"  >
                <input id="processNameOfDelete" type="text" name="processNameOfDelete" lay-verify="required" class="layui-input" style="margin-bottom: 10px ; width: 12em !important;" readonly="readonly">
                </div></div>
                
                <div class="layui-input-inline" style="margin: 5px;">
                <label class="layui-form-label">删除人：</label>
                <div class="layui-input-inline " style="width: 15em"  >
                <input id="deleteWorker" type="text" name="deleteWorker" lay-verify="required" class="layui-input" style="margin-bottom: 10px ;width: 14em !important;" readonly="readonly">
                </div></div>
                
                <div class="layui-input-inline" style="margin: 5px;">
                <label class="layui-form-label">删除时间：</label>
                <div class="layui-input-inline " style="width: 15em">
                <input id="nowTime" type="text" readonly="readonly"  lay-verify="required" name="nowTime"  class="layui-input" style="margin-bottom: 10px;width: 13em !important;"></div>
                </div>
                
                <div align="center" style="margin: 15px">
                <button id="deleteBtn" type="button" class="layui-btn" lay-submit="" lay-filter="deleteBtn" >确认删除</button>
                <button class="layui-btn layui-btn-danger" onclick="layer.close(layer.index)">取消</button></div>
          `,
                            success: function(){
                                $('#processNameOfDelete').val(res.data.processName);
                                $('#deleteWorker').val(res.data.userName);
                                var nowDate=new Date();
                                $('#nowTime').val(nowDate.toLocaleDateString()+' '+nowDate.toLocaleTimeString());
                                nowProcessId=res.data.nowProcessId;
                            }
                        });
                        form.on('submit(deleteBtn)', function() {
                            $.ajax({
                                url: url + '/report-order/deleteInfo',
                                method: 'POST',
                                contentType: 'application/json',
                                data: JSON.stringify({
                                    userId:sessionStorage.getItem('userId'),
                                    orderId:index,
                                    processId:nowProcessId
                                }),
                                success: function(res) {
                                    layer.closeAll();
                                    layer.msg(res.msg)
                                    search(1 );
                                }
                            })
                        })
                    }
                }
            })
            layer.close(confirm1);
        }
        ,function (){
            layer.close(confirm1);
        }
    )
}

function reProBtn(index) {
    let confirm1=layer.confirm('确定提交返工？', {
            btn: ['确认','取消']
        },function () {
            layer.open({
                type: 1,
                skin: 'layui-layer-rim',
                area: ['340px', '320px'],
                title: '提交返工',
                content: `
              <div class="layui-row" style="margin-bottom: 10px"></div>
                <div class="layui-input-inline" style="margin: 5px;">
                <label class="layui-form-label">工单号：</label>
                <div class="layui-input-inline " style="width: 15em"  >
                <input id="reProOrder" type="text" name="reProOrder" lay-verify="required" class="layui-input" style="margin-bottom: 10px ;width: 14em !important;" readonly="readonly">
                </div></div>
              
                <div class="layui-input-inline" style="margin: 5px;">
                <label class="layui-form-label">提交人：</label>
                <div class="layui-input-inline " style="width: 15em"  >
                <input id="reProWorker" type="text" name="reProWorker" lay-verify="required" class="layui-input" style="margin-bottom: 10px ;width: 14em !important;" readonly="readonly">
                </div></div>
                
                <div class="layui-input-inline" style="margin: 5px;">
                <label class="layui-form-label">提交时间：</label>
                <div class="layui-input-inline " style="width: 15em">
                <input id="reProTime" type="text" readonly="readonly"  lay-verify="required" name="reProTime"  class="layui-input" style="margin-bottom: 10px;width: 13em !important;"></div>
                </div>
                
                <div align="center" style="margin: 15px">
                <button id="subReProBtn" type="button" class="layui-btn" lay-submit="" lay-filter="subReProBtn" >确认提交</button>
                <button class="layui-btn layui-btn-danger" onclick="layer.close(layer.index)">取消</button></div>
          `,
                success: function(){
                    $('#reProOrder').val(index);
                    $('#reProWorker').val(sessionStorage.getItem('loginName'));
                    var nowDate=new Date();
                    $('#reProTime').val(nowDate.toLocaleDateString()+' '+nowDate.toLocaleTimeString());
                }
            });
            form.on('submit(subReProBtn)', function() {
                $.ajax({
                    url: url + '/re-order/addReOrder',
                    method: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify({
                        oldLot:index,
                        creater:sessionStorage.getItem('loginName')
                    }),
                    success: function(res) {
                        if(res.code==200){
                            layer.confirm(res.msg, {
                                btn: ['好的'], //按钮
                                icon: 1
                            }, function(){
                                parent.switchNav(2)
                            });
                        }else {
                            layer.closeAll();
                            layer.alert(res.msg, {
                                icon: 2,
                                skin: 'layer-ext-demo' //见：扩展说明
                            })
                        }
                    }
                })
            })
            layer.close(confirm1);
        }
        ,function (){
            layer.close(confirm1);
        }
    )
}

//检录方法
function checkInIt(index,index2) {
    $.ajax({
        url: url + '/work-order/checkInIt',
        method: 'POST',
        contentType: 'application/json',
        data: JSON.stringify({
            orderNum:index
        }),
        success: function(res) {
            if(res.code==400){
                layer.msg("该工单已完成检录！无需重复操作")
            }else{
                layer.open({
                    type: 1,
                    skin: 'layui-layer-rim',
                    area: ['390px', '370px'],
                    title: '工单首站检录',
                    content: `
              <div class="layui-row" style="margin-bottom: 10px"></div>
                <input id="orderId" type="text" name="id" style="display: none;">
                
                <div class="layui-input-inline" style="margin: 5px;">
                <label class="layui-form-label">检录的工单号：</label>
                <div class="layui-input-inline " style="width: 15em"  >
                <input id="orderNumOfCheck" type="text" name="orderNumOfCheck" lay-verify="required" class="layui-input" style="margin-bottom: 10px ; width: 14em !important;" readonly="readonly">
                </div></div>
                
                <div class="layui-input-inline" style="margin: 5px;">
                <label class="layui-form-label">检录的工序：</label>
                <div class="layui-input-inline " style="width: 15em"  >
                <input id="nowProcess" type="text" name="nowProcess" lay-verify="required" class="layui-input" style="margin-bottom: 10px ;width: 15em !important;" readonly="readonly">
                </div></div>
                
                <div class="layui-input-inline" style="margin: 5px;">
                <label class="layui-form-label">检录时间：</label>
                <div class="layui-input-inline " style="width: 15em">
                <input id="nowTime" type="text" readonly="readonly"  lay-verify="required" name="nowTime"  class="layui-input" style="margin-bottom: 10px;width: 16em !important;"></div>
                </div>
                
                <div align="center" style="margin: 20px">
                <button id="checkIn" type="button" class="layui-btn" lay-submit="" lay-filter="checkIn" >确认检录</button>
                <button class="layui-btn layui-btn-danger" onclick="layer.close(layer.index)">取消</button></div>
           
          `,
                    success: function(){
                        $('#orderNumOfCheck').val(index);
                        $('#nowProcess').val(index2);
                        var nowDate=new Date();
                        $('#nowTime').val(nowDate.toLocaleDateString()+' '+nowDate.toLocaleTimeString());
                    }
                });

                form.on('submit(checkIn)', function() {
                    let confirm1=layer.confirm('确认检录吗?</br>' +
                        '注意：该操作只能物料员进行！', {
                            btn: ['确认','取消']
                        },function () {
                            $.ajax({
                                url: url + '/work-order/checkIn',
                                method: 'POST',
                                contentType: 'application/json',
                                data: JSON.stringify({
                                    orderNum:index
                                }),
                                success: function(res) {
                                    if(res.code==200){
                                        parent.layer.alert(res.msg, {
                                            icon: 1,
                                            skin: 'layer-ext-demo', //见：扩展说明
                                            end: function(){
                                                layer.closeAll();
                                                search(1);
                                            }
                                        })
                                    }else {
                                        parent.layer.alert(res.msg, {
                                            icon: 2,
                                            skin: 'layer-ext-demo' ,//见：扩展说明
                                            end: function(){
                                                layer.closeAll();
                                                search(1 );
                                            }
                                        })
                                    }
                                }
                            })
                            layer.close(confirm1);
                        }
                        ,function (){
                            layer.close(confirm1);
                        }
                    )
                })

            }
        }
    })
}





